package com.wrater.zero.common.base;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel("固定返回格式")
public class ResultVo<T> {

    private final static Integer SUCCESS_CODE = 200;

    private final static String SUCCESS_MASSAGE = "成功";

    /**
     * 错误码
     */
    @ApiModelProperty("错误码")
    private Integer code;

    /**
     * 提示信息
     */
    @ApiModelProperty("提示信息")
    private String message;

    /**
     * 具体的内容
     */
    @ApiModelProperty("响应数据")
    private T data;

    public ResultVo(T data){
        this();
        this.data = data;
    }

    public ResultVo(){
        this.code = SUCCESS_CODE;
        this.message = SUCCESS_MASSAGE;
    }

    public ResultVo success(){
        ResultVo resultVo = new ResultVo();
        resultVo.setCode(200);
        resultVo.setMessage("成功");
        return resultVo;
    }

    public ResultVo success(T obj){
        ResultVo resultVo = this.success();
        resultVo.setData(obj);
        return resultVo;
    }

    public static ResultVo error(){
        return error(new BaseException(ResultExceptionEnum.DEFAULT_EXCEPTION, ResultVo.class.toString()));
    }

    public static ResultVo error(BaseException e){
        ResultVo resultVo = new ResultVo();
        resultVo.setCode(e.getCode());
        resultVo.setMessage(e.getLocalizedMessage());
        return resultVo;
    }

}